home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PsL Monthly 1993 December
/
PSL Monthly Shareware CD-ROM (December 1993).iso
/
prgmming
/
dos
/
c
/
ljl.exe
/
PORTDIR.C
< prev
next >
Wrap
C/C++ Source or Header
|
1992-09-12
|
8KB
|
323 lines
/***************************************************************************/
/* */
/* Copyright (c) 1990, Bob Withers */
/* */
/* Files to allow portable access to directory and file structure info */
/* across MSDOS and OS/2 compilers. */
/* */
/***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "portdir.h"
struct S_DateFmt
{
unsigned short day : 5;
unsigned short month : 4;
unsigned short year : 7;
};
typedef struct S_DateFmt DATEFMT;
struct S_TimeFmt
{
unsigned short twosecs : 5;
unsigned short minutes : 6;
unsigned short hours : 5;
};
typedef struct S_TimeFmt TIMEFMT;
union U_DateTimeCvt
{
unsigned short DateTime;
DATEFMT Date;
TIMEFMT Time;
};
typedef union U_DateTimeCvt DATETIMECVT;
static time_t CvtDosDateTime(unsigned short usDate, unsigned short usTime);
#ifdef __TURBOC__
#include <dir.h>
#include <dos.h>
struct S_DirHandle
{
short sCnt;
unsigned uAttr;
struct ffblk ff;
DIRECTENTRY DirectEntry;
char path[1];
};
typedef struct S_DirHandle DIRHANDLE;
void *DirOpen(char *pathname, unsigned uAttr)
{
auto DIRHANDLE *pDirHandle;
pDirHandle = malloc(sizeof(DIRHANDLE) + strlen(pathname));
if (pDirHandle)
{
pDirHandle->sCnt = 0;
pDirHandle->uAttr = uAttr;
strcpy(pDirHandle->path, pathname);
}
return(pDirHandle);
}
DIRECTENTRY *DirRead(void *pHandle)
{
auto int nRC;
auto DIRHANDLE *pDirHandle;
if (NULL == pHandle)
return(NULL);
pDirHandle = pHandle;
if (0 == pDirHandle->sCnt++)
nRC = findfirst(pDirHandle->path, &pDirHandle->ff,
pDirHandle->uAttr);
else
nRC = findnext(&pDirHandle->ff);
if (0 != nRC)
return(NULL);
pDirHandle->DirectEntry.d_attr = pDirHandle->ff.ff_attrib;
pDirHandle->DirectEntry.d_datetime =
CvtDosDateTime(pDirHandle->ff.ff_fdate,
pDirHandle->ff.ff_ftime);
pDirHandle->DirectEntry.d_filesize = pDirHandle->ff.ff_fsize;
pDirHandle->DirectEntry.d_namlen = strlen(pDirHandle->ff.ff_name);
strcpy(pDirHandle->DirectEntry.d_name, pDirHandle->ff.ff_name);
return(&pDirHandle->DirectEntry);
}
void DirClose(void *pHandle)
{
free(pHandle);
return;
}
#endif /* __TURBOC__ */
#ifdef _MSC_VER
#ifdef OS2
#define INCL_BASE
#include <os2.h>
struct S_DirHandle
{
SHORT sCnt;
USHORT uAttr;
HDIR hDir;
DIRECTENTRY DirectEntry;
CHAR path[1];
};
typedef struct S_DirHandle DIRHANDLE;
void *DirOpen(char *pathname, unsigned uAttr)
{
auto DIRHANDLE *pDirHandle;
pDirHandle = malloc(sizeof(DIRHANDLE) + strlen(pathname));
if (pDirHandle)
{
pDirHandle->sCnt = 0;
pDirHandle->uAttr = uAttr;
pDirHandle->hDir = HDIR_CREATE;
strcpy(pDirHandle->path, pathname);
}
return(pDirHandle);
}
DIRECTENTRY *DirRead(void *pHandle)
{
auto USHORT usRC;
auto USHORT usSearchCnt = 1;
auto FILEFINDBUF ff;
auto DIRHANDLE *pDirHandle;
auto DATETIMECVT Cvt;
auto unsigned short usDate, usTime;
if (NULL == pHandle)
return(NULL);
pDirHandle = pHandle;
if (0 == pDirHandle->sCnt++)
usRC = DosFindFirst(pDirHandle->path, &pDirHandle->hDir,
pDirHandle->uAttr, &ff, sizeof(ff),
&usSearchCnt, 0L);
else
usRC = DosFindNext(pDirHandle->hDir, &ff, sizeof(ff), &usSearchCnt);
if (usRC || 0 == usSearchCnt)
return(NULL);
pDirHandle->DirectEntry.d_attr = ff.attrFile;
Cvt.Date.day = ff.fdateLastWrite.day;
Cvt.Date.month = ff.fdateLastWrite.month;
Cvt.Date.year = ff.fdateLastWrite.year;
usDate = Cvt.DateTime;
Cvt.Time.twosecs = ff.ftimeLastWrite.twosecs;
Cvt.Time.minutes = ff.ftimeLastWrite.minutes;
Cvt.Time.hours = ff.ftimeLastWrite.hours;
usTime = Cvt.DateTime;
pDirHandle->DirectEntry.d_datetime = CvtDosDateTime(usDate, usTime);
pDirHandle->DirectEntry.d_filesize = ff.cbFile;
pDirHandle->DirectEntry.d_namlen = ff.cchName;
memcpy(pDirHandle->DirectEntry.d_name, ff.achName, ff.cchName);
pDirHandle->DirectEntry.d_name[ff.cchName] = '\0';
return(&pDirHandle->DirectEntry);
}
void DirClose(void *pHandle)
{
free(pHandle);
return;
}
#else /* MSDOS */
#include <direct.h>
#include <dos.h>
struct S_DirHandle
{
short sCnt;
unsigned uAttr;
struct find_t ff;
DIRECTENTRY DirectEntry;
char path[1];
};
typedef struct S_DirHandle DIRHANDLE;
void *DirOpen(char *pathname, unsigned uAttr)
{
auto DIRHANDLE *pDirHandle;
pDirHandle = (DIRHANDLE *) malloc(sizeof(DIRHANDLE) + strlen(pathname));
if (pDirHandle)
{
pDirHandle->sCnt = 0;
pDirHandle->uAttr = uAttr;
strcpy(pDirHandle->path, pathname);
}
return(pDirHandle);
}
DIRECTENTRY *DirRead(void *pHandle)
{
auto unsigned uRC;
auto DIRHANDLE *pDirHandle;
if (NULL == pHandle)
return(NULL);
pDirHandle = (DIRHANDLE *) pHandle;
if (0 == pDirHandle->sCnt++)
uRC = _dos_findfirst(pDirHandle->path, pDirHandle->uAttr,
&pDirHandle->ff);
else
uRC = _dos_findnext(&pDirHandle->ff);
if (0 != uRC)
return(NULL);
pDirHandle->DirectEntry.d_attr = pDirHandle->ff.attrib;
pDirHandle->DirectEntry.d_datetime =
CvtDosDateTime(pDirHandle->ff.wr_date,
pDirHandle->ff.wr_time);
pDirHandle->DirectEntry.d_filesize = pDirHandle->ff.size;
pDirHandle->DirectEntry.d_namlen = strlen(pDirHandle->ff.name);
strcpy(pDirHandle->DirectEntry.d_name, pDirHandle->ff.name);
return(&pDirHandle->DirectEntry);
}
void DirClose(void *pHandle)
{
free(pHandle);
return;
}
#endif /* OS2 */
#endif /* _MSC_VER */
static time_t CvtDosDateTime(unsigned short usDate, unsigned short usTime)
{
auto DATETIMECVT dt;
auto struct tm tm;
dt.DateTime = usTime;
tm.tm_sec = dt.Time.twosecs * 2;
tm.tm_min = dt.Time.minutes;
tm.tm_hour = dt.Time.hours;
dt.DateTime = usDate;
tm.tm_mday = dt.Date.day;
tm.tm_mon = dt.Date.month;
tm.tm_year = dt.Date.year + 80;
tm.tm_wday = 0;
tm.tm_yday = 0;
tm.tm_isdst = 0;
return(mktime(&tm));
}
/* UNIX compatibility */
DIR *opendir(char *pathname)
{
return(DirOpen(pathname, FILEATTR_NORMAL));
}
struct dirent *readdir(DIR *pDir)
{
return((struct dirent *) DirRead(pDir));
}
void closedir(DIR *pDir)
{
DirClose(pDir);
return;
}